#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N];
bool check_leap(int z) { return z % 4 == 0; }
bool check_date(int x, int y, int z) {
  static int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  if (y > 12) return false;
  if (y == 2 && check_leap(z)) return x <= 29;
  if (x > days[y]) return false;
  return true;
}
bool check(int x1, int y1, int z1, int x, int y, int z) {
  if (!check_date(x1, y1, z1)) return false;
  if (z != z1 + 18) return z > z1 + 18;
  if (y != y1) return y > y1;
  return x >= x1;
}
void solve() {
  int x, y, z;
  scanf("%02d.%02d.%02d", &x, &y, &z);
  int x1, y1, z1;
  scanf("%02d.%02d.%02d", &x1, &y1, &z1);
  if (check(x1, y1, z1, x, y, z))
    cout << "YES" << endl;
  else if (check(x1, z1, y1, x, y, z))
    cout << "YES" << endl;
  else if (check(y1, x1, z1, x, y, z))
    cout << "YES" << endl;
  else if (check(y1, z1, x1, x, y, z))
    cout << "YES" << endl;
  else if (check(z1, x1, y1, x, y, z))
    cout << "YES" << endl;
  else if (check(z1, y1, x1, x, y, z))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  t = 1;
  while (t--) solve();
  return 0;
}